package com.zdkj.subway.support.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.support.entity.SwPmDeviceOperationCodeRecord;
import com.zdkj.subway.support.service.SwPmDeviceOperationCodeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 设备操作号历史使用记录表
 *
 * @author cuilei
 * @date 2020-06-03 15:33:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swpmdeviceoperationcoderecord")
@Api(value = "swpmdeviceoperationcoderecord", tags = "设备操作号历史使用记录表管理")
public class SwPmDeviceOperationCodeRecordController {

    private final SwPmDeviceOperationCodeRecordService swPmDeviceOperationCodeRecordService;

    /**
     * 分页查询
     *
     * @param page                          分页对象
     * @param swPmDeviceOperationCodeRecord 设备操作号历史使用记录表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwPmDeviceOperationCodeRecordPage(Page page, SwPmDeviceOperationCodeRecord swPmDeviceOperationCodeRecord) {
        QueryWrapper<SwPmDeviceOperationCodeRecord> queryWrapper = Wrappers.query(swPmDeviceOperationCodeRecord);
        if (StringUtils.isNotEmpty(swPmDeviceOperationCodeRecord.getOperationCode())) {
            queryWrapper.like("OPERATION_CODE", swPmDeviceOperationCodeRecord.getOperationCode());
            swPmDeviceOperationCodeRecord.setOperationCode(null);
        }
        if (!SecurityUtils.getUser().getBelongToLine()) {
            queryWrapper.lambda().eq(SwPmDeviceOperationCodeRecord::getLineCode, SecurityUtils.getUser().getLineCode());
            queryWrapper.lambda().eq(SwPmDeviceOperationCodeRecord::getStationCode, SecurityUtils.getUser().getStationCode());
            return R.ok(swPmDeviceOperationCodeRecordService.page(page, queryWrapper));
        }
        return R.ok(swPmDeviceOperationCodeRecordService.page(page, Wrappers.query(swPmDeviceOperationCodeRecord)));
    }

    /**
     * 通过id查询设备操作号历史使用记录表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swPmDeviceOperationCodeRecordService.getById(id));
    }

    /**
     * 新增设备操作号历史使用记录表
     *
     * @param swPmDeviceOperationCodeRecord 设备操作号历史使用记录表
     * @return R
     */
    @ApiOperation(value = "新增设备操作号历史使用记录表", notes = "新增设备操作号历史使用记录表")
    @SysLog("新增设备操作号历史使用记录表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_device_operation_code_record_add')")
    public R save(@RequestBody SwPmDeviceOperationCodeRecord swPmDeviceOperationCodeRecord) {
        SwPmDeviceOperationCodeRecord defaultObj = ObjectUtils.getDefaultObj(SwPmDeviceOperationCodeRecord.class);
        BeanUtils.copyProperties(swPmDeviceOperationCodeRecord, defaultObj);
        defaultObj.setLineCode(swPmDeviceOperationCodeRecord.getLineCode());
        defaultObj.setStationCode(swPmDeviceOperationCodeRecord.getStationCode());
        return R.ok(swPmDeviceOperationCodeRecordService.save(defaultObj));
    }

    /**
     * 新增设备操作号历史使用记录表--用于调用
     *
     * @param swPmDeviceOperationCodeRecord 设备操作号历史使用记录表
     * @return R
     */
    @ApiOperation(value = "新增设备操作号历史使用记录表", notes = "新增设备操作号历史使用记录表")
    @PostMapping("/innerSave")
    @Inner
    public R innerSave(@RequestBody SwPmDeviceOperationCodeRecord swPmDeviceOperationCodeRecord) {
        return R.ok(swPmDeviceOperationCodeRecordService.save(swPmDeviceOperationCodeRecord));
    }

    /**
     * 修改设备操作号历史使用记录表
     *
     * @param swPmDeviceOperationCodeRecord 设备操作号历史使用记录表
     * @return R
     */
    @ApiOperation(value = "修改设备操作号历史使用记录表", notes = "修改设备操作号历史使用记录表")
    @SysLog("修改设备操作号历史使用记录表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_device_operation_code_record_edit')")
    public R updateById(@RequestBody SwPmDeviceOperationCodeRecord swPmDeviceOperationCodeRecord) {
        return R.ok(swPmDeviceOperationCodeRecordService.updateById(swPmDeviceOperationCodeRecord));
    }

    /**
     * 通过id删除设备操作号历史使用记录表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除设备操作号历史使用记录表", notes = "通过id删除设备操作号历史使用记录表")
    @SysLog("通过id删除设备操作号历史使用记录表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_pm_device_operation_code_record_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swPmDeviceOperationCodeRecordService.removeById(id));
    }

}
